package com.xt.LibaryDeskManager.filter;

import com.xt.LibaryDeskManager.dto.UserMessage;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PromessIncepter implements HandlerInterceptor {

    /**
     * 前置拦截,拦截前端控制器的请求

     * @param handler
     * @return boolean 表达拦截请求后是否放行,true就是放行,接着执行后面拦截器或处理器;false请求就截断
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest req1, HttpServletResponse resp1, Object handler) throws Exception {
        UserMessage user= (UserMessage) req1.getSession().getAttribute("user");
        if (user==null){
            resp1.sendRedirect("/error1.jsp");
        }else{
            String url= String.valueOf(req1.getRequestURL());
            if (url.indexOf("/page/admin")!=-1&&user.getLevel()==3){
               return true;
            }else if(url.indexOf("/page/teacher")!=-1&&user.getLevel()==2){
                return true;
            }else if(url.indexOf("page/student")!=-1&&user.getLevel()==1){
                return true;
            }else if (url.indexOf("page/common")!=-1){
                return true;
            }else{
                resp1.sendRedirect("/error2.jsp");
            }
        }
        return false;
    }

}
